$(function(){

    var orderId,showMoney;

    // 页面初始化...执行获取接口数据的函数....
    getOrderDetail($.getUrlParam('orderId'));

    // 根据订单的id来查询订单的详情.....向后台发送请求....获取接口当中的数据..
    function getOrderDetail(orderId)
    {
        $.ajax({
            async:false,
            cache:false,
            type:'post',
            headers: {"Accept-CMS-Token":token},
            url:refundorderdetails,
            dataType:'json',
            data:{
                refundID:orderId
            },
            success:successFn,
            error:function(){
                console.log('加载部门列表接口请求失败')
            },
        });
    };

    // 跟后台ajax请求接口....成功的时候就执行下面的这个函数...
    function successFn(data)
    {
        if( data.resultCode == 200 )
        {
            orderId = data.value.refundRecord.id;
            $('.orderStatus').html( changeRefuseStatus( data.value.refundRecord.status,data.value.refundRecord.auditStatus ) );//订单状态
            if( data.value.refundRecord.status == 0 )
            {
                $('.apply_refuse').show();
            }
            else
            {
                $('.apply_refuse').hide();
            }
            $('.backType').html( changeServiceWord( data.value.refundRecord.backType) );//服务方式
            $('.reasons').html(  data.value.refundRecord.reasons );//退款原因
            $('.refuseType').html( '钱款原路返回' );//退款方式

            $('span.receiveName').html(  data.value.refundRecord.receiveName  );//买家信息
            $('span.receivePhone').html(  data.value.refundRecord.receivePhone  );//联系电话
            $('span.receiveAddress').html(  data.value.refundRecord.receiveAddress  );//收货地址
            console.log(data);
            // 展示商品信息
            clearDetailTable('.look_refuseMoney_detail');
            //每一条订单所对应的商品的状态是通过:data.value.refundRecord.status来判断共有三种状态.....
            $('.look_refuseMoney_detail').append('<tr></tr>');
            $('.look_refuseMoney_detail tr').eq(1).append('<td>1</td>');
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.commodity.name)+'</td>');//商品名称
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.commodity.number)+'</td>');//商品编号
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.commodity.colorName)+'</td>');//颜色
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.commodity.modelName)+'</td>');//型号
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.commodity.sizeName)+'</td>');//尺寸
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.refundRecord.amount)+'</td>');//数量
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.commodity.price)+'</td>');//单价
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+(data.value.refundRecord.amount)*(data.value.commodity.price)+'</td>');//总价
            $('.look_refuseMoney_detail tr').eq(1).append('<td>'+changeRefuseOrder(data.value.refundRecord.status,data.value.refundRecord.auditStatus)+'</td>');//订单状态
            //展示订单信息
            $('span.totalMoney').html( data.value.refundRecord.amount * data.value.commodity.price );//订单总金额
            $('span.expressMoney').html( 0 );//运费
            $('span.fundsReceived').html( data.value.refundRecord.amount * data.value.commodity.price );//实收款
            showMoney = data.value.refundRecord.amount * data.value.commodity.price;//这个参数用于弹框的展示....
                $('span.orderNum').html( data.value.refundRecord.shopOrderNum );//订单编号
            $('span.payOrderNum').html( data.value.refundRecord.payOutNum );//支付交易号
            $('span.createTime').html( getMoth(data.value.refundRecord.createTime) );//申请退款时间...
            $('span.payTime').html( getMoth(data.value.refundRecord.payTime) );//...支付订单时间

        }
        else if( data.resultCode == 515 )
        {
            reLogin();
        }
        else
        {
            console.log('接口出错!非200,非515!')
        };
        console.log('查询订单详情接口调通!!!!')
    };

    // 点击拒绝...拒绝这一条退款的订单...当用户点击拒绝的时候...点击完之后订单按照正常的流程开始走...订单状态变成了待发货订单...跳转到待发货的页面...
    $('.reject_apply').click(function(){

        $.ajax({
            type:'get',
            headers: {"Accept-CMS-Token":token},
            url:refundaudit,
            dataType:'json',
            data:{
                refundID:orderId,
                type:1
            },
            success:function(data){
                if(data.resultCode == 200)
                {
                    alert('拒绝成功!');
                    window.location.href = wait_goods;
                }
                if(data.resultCode == 500)
                {
                    alert('拒绝失败!');
                }
                else if(data.resultCode == 515)
                {
                    reLogin();
                };
                console.log(data);

            },
            error:function(){
                console.log('加载部门列表接口请求失败')
            },

        });

    });

    // 点击同意的按钮...跳出弹框...
    $('.not_sure_agree').click(function(){

        $('.mask-change').show();
        $('.get_money span').html(showMoney);
        $('.agree_refuse').show();
        // console.log(showMoney)
    });

    // 点击确定...将钱款按原路返回...跟后台做交互...
    $('.sure_refuse_money').click(function(){

        $.ajax({
            async:false,
            type:'get',
            headers: {"Accept-CMS-Token":token},
            url:refundaudit,
            dataType:'json',
            data:{
                refundID:orderId,
                type:0
            },
            success:function(data){
                if(data.resultCode == 200)
                {
                    $('.orderStatus').html('退款完成');
                    // 隐藏页面中的一些状态
                    $('.apply_refuse').hide();
                    $('.mask-change').hide();
                    $('.agree_refuse').hide();
                }
                else if( data.resultCode == 500 )
                {
                    alert('退款失败!');
                    $('.mask-change').hide();
                    $('.agree_refuse').hide();
                    return false;
                }
                else if(data.resultCode == 515)
                {
                    reLogin();
                };
                console.log(data);

            },
            error:function(){
                console.log('加载部门列表接口请求失败')
            },

        });


    });




    // alert($.getUrlParam('orderId'));
});